﻿using System;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CreateJsonOrXml
{
    public partial class Form1 : Form
    {
        private TreeNode currentCopyTreeNode { get; set; }

        public Form1()
        {
            InitializeComponent();
            var newChild= this.treeView1.Nodes.Add("new");
            this.treeView1.SelectedNode = newChild;
            this.treeView1.NodeMouseClick += TreeView1_NodeMouseClick;
            newChild.BeginEdit();
            newChild.Tag = "new value";
            this.treeView1.AfterSelect += TreeView1_AfterSelect;
            this.addRootBtn.Click += AddRootBtn_Click;
            this.addChildBtn.Click += AddChildBtn_Click;
            //double a;
            //MessageBox.Show(double.TryParse("12.5sd",out a).ToString());
        }

        private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //throw new NotImplementedException();
            this.treeView1.SelectedNode = e.Node;
        }

        private void AddChildBtn_Click(object sender, EventArgs e)
        {
            var newChild = this.treeView1.SelectedNode.Nodes.Add("new");
            this.treeView1.SelectedNode = newChild;
            newChild.BeginEdit();
            newChild.Tag = "new value";
        }

        private void AddRootBtn_Click(object sender, EventArgs e)
        {
            var newNode = this.treeView1.Nodes.Add("new");
            this.treeView1.SelectedNode = newNode;
            newNode.BeginEdit();
            newNode.Tag = "new value";
            //throw new NotImplementedException();
        }

        private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //MessageBox.Show("Test");
            Console.WriteLine(sender.ToString());
            this.comboBox1.Text = "";
            this.comboBox1.Text = e.Node.Tag != null ? e.Node.Tag.ToString() : (e.Node.Tag = "null").ToString();

            //throw new NotImplementedException();
        }

        private void createBtn_Click(object sender, EventArgs e)
        {
            var nodes = this.treeView1.Nodes;
            string str = CheckNodesForeach(nodes);
            Console.WriteLine(str);
            this.Write2File(str, @"..\template.configjson");
        }


        private void deleteNodeBtn_Click(object sender, EventArgs e)
        {
            if(this.treeView1.Nodes.Count!=0)
                this.treeView1.SelectedNode.Remove();
        }

        private int depth = 0;
        private double tempNumber;
        private bool tempBool;
        private string CheckNodesForeach(TreeNodeCollection nodes)
        {
            int count = nodes.Count;
            TreeNode lastNode;
            StringBuilder strB = new StringBuilder();
            string tempStr;
            for (int i = 0; i < depth; i++)
                for (int j = 0; j < 4; j++)
                    strB.Append(" ");
            strB.Append("{\n");
          
            foreach (TreeNode item in nodes)
            {
                //bool.
                if (double.TryParse(item.Tag.ToString(), out tempNumber)
                    || bool.TryParse(item.Tag.ToString(), out tempBool)
                    || item.Tag.ToString() == "null"
                    || item.Tag.ToString() == "Null"
                    || item.Tag.ToString() == "NULL")
                {
                    tempStr = item.Tag.ToString().Trim();
                }
                else
                {
                    tempStr = "\"" + item.Tag.ToString().Trim() + "\"";
                }

                if (item.Nodes.Count == 0)
                {
                    count--;
                    for (int i = 0; i < depth + 1; i++)
                        for (int j = 0; j < 4; j++)
                            strB.Append(" ");
                    if ((item.Parent != null && item.Parent.LastNode.Equals(item)) || item.Index == nodes.Count - 1)
                    {
                        strB.Append("\"" + item.Text + "\":" + tempStr + "\n");
                        if (item.Index == nodes.Count - 1)
                            lastNode = item;
                    }
                    else
                        strB.Append("\"" + item.Text + "\":" + tempStr + ",\n");

                    continue;
                }
                else
                {
                    for (int i = 0; i < depth + 1; i++)
                        for (int j = 0; j < 4; j++)
                            strB.Append(" ");
                    strB.Append("\"" + item.Text + "\":\n");
                    depth++;
                    var str = CheckNodesForeach(item.Nodes);
                    depth--;
                    strB.Append(str);
                    if (item.Index < nodes.Count - 1)
                        strB.Append(",\n");
                    else
                        strB.Append("\n");
                }
            }
            for (int i = 0; i < depth; i++)
                for(int j=0; j<4;j++)
                    strB.Append(" ");

            strB.Append("}");
           
            return strB.ToString();
        }


        private void comboBox1_Leave(object sender, EventArgs e)
        {
            //MessageBox.Show(this);
            this.treeView1.SelectedNode.Tag = this.comboBox1.Text.Trim();
        }


      

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.currentCopyTreeNode = (TreeNode)this.treeView1.SelectedNode.Clone();
            //Console.WriteLine(sender.ToString());
        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.treeView1.SelectedNode.Nodes.Add((TreeNode)this.currentCopyTreeNode.Clone());
        }
        //private void 

        private void Write2File(string content, string path)
        {
            try
            {
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.Write(content);
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
            
        }
    }
}
